package com.xunshangwang.pinhaohuo.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.ui.fragments.BaseFragment;
import com.xunshangwang.pinhaohuo.ui.fragments.HomeFragment;
import com.xunshangwang.pinhaohuo.ui.fragments.MyGroupFragment;
import com.xunshangwang.pinhaohuo.ui.fragments.PersonalCenterFragment;
import com.xunshangwang.pinhaohuo.ui.fragments.PinFragment;

import tool.library.ToastTool;

/**
 * Created by xzp on 16/1/14.
 */
public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener{
    private RadioGroup mNavigationGroup;
    private HomeFragment mHomeFragment;
    private BaseFragment mCurrFragment;
    private MyGroupFragment mMyGroupFragment;
    private PersonalCenterFragment mCenterFragment;
    private PinFragment mPinFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNavigationGroup= (RadioGroup) this.findViewById(R.id.main_navigation_bar);
        mNavigationGroup.setOnCheckedChangeListener(this);
        if(savedInstanceState==null)
            mNavigationGroup.check(R.id.main_navigation_home_btn);
        else
        {
            FragmentManager manager=getSupportFragmentManager();
            FragmentTransaction transaction=manager.beginTransaction();
            mHomeFragment= (HomeFragment) manager.findFragmentByTag("home");
            if(mHomeFragment!=null)
                transaction.hide(mHomeFragment);
            mCenterFragment= (PersonalCenterFragment) manager.findFragmentByTag("center");
            if(mCenterFragment!=null)
                transaction.hide(mCenterFragment);
            mMyGroupFragment= (MyGroupFragment) manager.findFragmentByTag("group");
            if(mMyGroupFragment!=null)
                transaction.hide(mMyGroupFragment);
            mPinFragment= (PinFragment) manager.findFragmentByTag("pin");
            if(mPinFragment!=null)
                transaction.hide(mPinFragment);
            mCurrFragment= (BaseFragment) manager.getFragment(savedInstanceState,"curr");

            if(mCurrFragment!=null)
                transaction.show(mCurrFragment);
            transaction.commit();
        }
        checkAuthError(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        checkAuthError(intent);
    }

    private void checkAuthError(Intent intent){
        boolean authError=intent.getBooleanExtra("AuthError",false);
        if(authError){
            getMainApplication().setAccount(null);
            if(mNavigationGroup.getCheckedRadioButtonId()==R.id.main_navigation_group_btn){
                RadioButton button= (RadioButton) mNavigationGroup.findViewById(
                        R.id.main_navigation_personal_center_btn);
                button.setChecked(true);
            }
            logout();
            if(mCenterFragment!=null && mCenterFragment.getView()!=null){
                mCenterFragment.logout();
            }
        }
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(!getMainApplication().isLogin() && checkedId==R.id.main_navigation_group_btn){
            RadioButton button= (RadioButton) group.findViewById(
                    R.id.main_navigation_personal_center_btn);
            button.setChecked(true);
            return;
        }
        FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        if(mCurrFragment!=null)
            transaction.hide(mCurrFragment);
        switch (checkedId){
            case R.id.main_navigation_home_btn:
                if(mHomeFragment==null){
                    mHomeFragment=new HomeFragment();
                    transaction.add(R.id.fragment_container,mHomeFragment,"home");
                } else
                    transaction.show(mHomeFragment);
                mCurrFragment=mHomeFragment;
                break;
            case R.id.main_navigation_personal_center_btn:
                if(mCenterFragment==null) {
                    mCenterFragment = new PersonalCenterFragment();
                    transaction.add(R.id.fragment_container,mCenterFragment,"center");
                } else {
                    transaction.show(mCenterFragment);
                }
                mCurrFragment=mCenterFragment;
                break;
            case R.id.main_navigation_group_btn:
                if(mMyGroupFragment==null){
                    mMyGroupFragment=new MyGroupFragment();
                    transaction.add(R.id.fragment_container,mMyGroupFragment,"group");
                }else {
                    transaction.show(mMyGroupFragment);
                }
                mCurrFragment=mMyGroupFragment;
                break;
            case R.id.main_navigation_pin_btn:
                if(mPinFragment==null){
                    mPinFragment=new PinFragment();
                    transaction.add(R.id.fragment_container,mPinFragment,"pin");
                }else {
                    transaction.show(mPinFragment);
                }
                mCurrFragment=mPinFragment;
                break;
        }
        transaction.commit();
    }
    public void logout(){
        if(mMyGroupFragment!=null){
            FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
            transaction.remove(mMyGroupFragment);
            transaction.commitAllowingStateLoss();
            mMyGroupFragment=null;
        }
    }

    private long mBackPressedTime=0;
    @Override
    public void onBackPressed() {
        long nowTime=System.currentTimeMillis();
        if(nowTime-mBackPressedTime<2000) {
            super.onBackPressed();
        }else {
            mBackPressedTime=nowTime;
            ToastTool.showToast("再按一次退出程序");
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getSupportFragmentManager().putFragment(outState,"curr",mCurrFragment);
    }
}
